package com.liush.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liush.common.result.Result;
import com.liush.model.controller.SysRole;
import com.liush.model.vo.AssginRoleVo;
import com.liush.model.vo.SysRoleQueryVo;
import com.liush.system.annotation.Log;
import com.liush.system.enums.BusinessType;
import com.liush.system.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色 前端控制器
 * </p>
 *
 * @author liush
 * @since 2023-02-25
 */
@Api(tags = "角色管理")
@RestController
@RequestMapping("/admin/system/sysRole")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    //查询所有的角色信息 使用result统一返回
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation(value = "获取全部角色列表")
    @GetMapping("/findAll")
    public Result<List<SysRole>> findAll() {
        List<SysRole> roleList = sysRoleService.list();
        return Result.ok(roleList);
    }

    //分页查询
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation("分页查询")
    @GetMapping("/{pageNum}/{pageSize}")
    public Result index(
            @ApiParam(name = "pageNum", value = "当前页码", readOnly = true)
            @PathVariable Long pageNum,
            @ApiParam(name = "pageSize", value = "每页显示行数", readOnly = true)
            @PathVariable Long pageSize,
            @ApiParam(name = "sysRoleQueryVo", value = "角色查询条件", readOnly = false)
                    SysRoleQueryVo sysRoleQueryVo
    ) {
        //1.使用mybatis-plus 默认的分页方法
        /*Page<SysRole> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();
        String roleName = sysRoleQueryVo.getRoleName();
        queryWrapper.like(roleName == null ? false : true, SysRole::getRoleName, roleName);
        Page<SysRole> sysRoleListPage = sysRoleService.page(page, queryWrapper);*/

        //2.自定义分页方法
        Page page = new Page(pageNum, pageSize);
        Page<SysRole> pageRole = sysRoleService.findPage(page, sysRoleQueryVo);
        return Result.ok(pageRole);
    }

    //根据id查询角色信息
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation("根据id查询角色信息")
    @GetMapping("/get/{id}")
    public Result get(
            @ApiParam(name = "id", value = "角色主键", required = true)
            @PathVariable Long id) {
        SysRole sysRole = null;
        sysRole = sysRoleService.getById(id);
        /*try {
            System.out.println(1 / 0);
            sysRole = sysRoleService.getById(id);
        } catch (Exception e) {
            e.printStackTrace();
            throw new GuiguException(666, "自定义异常错误信息");
        }*/
        return Result.ok(sysRole);
    }

    //添加角色信息
    @Log(title = "角色管理", businessType = BusinessType.INSERT)
    @PreAuthorize("hasAuthority('bnt.sysRole.add')")
    @ApiOperation("添加角色信息")
    @PostMapping("/save")
    public Result save(
            @ApiParam(name = "sysRole", value = "角色对象", required = true)
            @RequestBody SysRole sysRole
    ) {
        sysRoleService.save(sysRole);
        return Result.ok();
    }

    //修改角色信息
    @Log(title = "角色管理", businessType = BusinessType.UPDATE)
    @PreAuthorize("hasAuthority('bnt.sysRole.update')")
    @ApiOperation("修改角色信息")
    @PutMapping("/update")
    public Result update(
            @ApiParam(name = "sysRole", value = "修改的角色对象", required = true)
            @RequestBody SysRole sysRole
    ) {
        sysRole.setUpdateTime(null);
        sysRoleService.updateById(sysRole);
        return Result.ok();
    }

    //根据id删除单条数据
    @Log(title = "角色管理", businessType = BusinessType.DELETE)
    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @ApiOperation("根据id删除单条数据")
    @DeleteMapping("/remove/{id}")
    public Result remove(
            @ApiParam(name = "id", value = "角色主键,删除的条件", required = true)
            @PathVariable Long id) {
        sysRoleService.removeById(id);
        return Result.ok();
    }

    //根据id批量删除多条数据
    @Log(title = "角色管理", businessType = BusinessType.DELETE)
    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @ApiOperation("根据id批量删除多条数据")
    @DeleteMapping("/batchRemove")
    public Result batchRemove(
            @ApiParam(name = "ids", value = "角色主键,删除的条件", required = true)
            @RequestBody List<Long> ids
    ) {
        sysRoleService.removeByIds(ids);
        return Result.ok();
    }

    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation("获取用户的角色信息")
    @GetMapping("/toAssign/{userId}")
    public Result toAssign(
            @ApiParam(name = "userId", value = "用户id")
            @PathVariable Long userId) {
        Map mapRolesByUserId = sysRoleService.getRolesByUserId(userId);
        return Result.ok(mapRolesByUserId);
    }

    @Log(title = "角色管理", businessType = BusinessType.ASSIGN)
    @PreAuthorize("hasAuthority('bnt.sysUser.assignRole')")
    @ApiOperation(value = "为用户分配角色")
    @PostMapping("/doAssign")
    public Result assignRoles(
            @ApiParam(name = "assginRoleVo", value = "用户和角色id结果集")
            @RequestBody AssginRoleVo assginRoleVo
    ) {
        sysRoleService.doAssignRole(assginRoleVo);
        return Result.ok();
    }
}
